using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Terra.Core.Common.Enums.Identity;
using Terra.Data.Entities;

namespace Terra.Data.Entities.Identity;

/// <summary>
/// 用户实体
/// </summary>
[Table("Users")]
[Comment("用户信息表")]
[Index(nameof(UserName), IsUnique = true)]
[Index(nameof(NormalizedUserName), IsUnique = true)]
[Index(nameof(Email))]
[Index(nameof(NormalizedEmail))]
[Index(nameof(PhoneNumber))]
public class User : AuditEntityBase<Guid>
{
    /// <summary>
    /// 用户名
    /// </summary>
    [Required]
    [MaxLength(256)]
    [Comment("用户登录名，唯一")]
    public string UserName { get; set; }

    /// <summary>
    /// 规范化用户名
    /// </summary>
    [Required]
    [MaxLength(256)]
    [Comment("用户名大写格式，用于不区分大小写查询")]
    public string NormalizedUserName { get; set; }

    /// <summary>
    /// 电子邮件
    /// </summary>
    [MaxLength(256)]
    [Comment("电子邮件地址")]
    public string Email { get; set; }

    /// <summary>
    /// 规范化电子邮件
    /// </summary>
    [MaxLength(256)]
    [Comment("电子邮件大写格式，用于不区分大小写查询")]
    public string NormalizedEmail { get; set; }

    /// <summary>
    /// 邮箱已确认
    /// </summary>
    [Comment("邮箱是否已验证")]
    public bool EmailConfirmed { get; set; }

    /// <summary>
    /// 密码哈希
    /// </summary>
    [MaxLength(1024)]
    [Comment("密码哈希值，使用PBKDF2加密")]
    public string PasswordHash { get; set; }

    /// <summary>
    /// 密码盐值
    /// </summary>
    [MaxLength(256)]
    [Comment("密码盐值，用于密码重置和令牌验证")]
    public string PasswordSalt { get; set; }

    /// <summary>
    /// 安全戳
    /// </summary>
    [MaxLength(256)]
    [Comment("安全戳，用于密码重置和令牌验证")]
    public string SecurityStamp { get; set; }

    /// <summary>
    /// 并发标记
    /// </summary>
    [MaxLength(256)]
    [ConcurrencyCheck]
    [Comment("并发标记，用于乐观锁")]
    public string ConcurrencyStamp { get; set; }

    /// <summary>
    /// 手机号码
    /// </summary>
    [MaxLength(32)]
    [Comment("手机号码")]
    public string PhoneNumber { get; set; }

    /// <summary>
    /// 手机号码已确认
    /// </summary>
    [Comment("手机号是否已验证")]
    public bool PhoneNumberConfirmed { get; set; }

    /// <summary>
    /// 启用双因素认证
    /// </summary>
    [Comment("是否启用双因素认证")]
    public bool TwoFactorEnabled { get; set; }

    /// <summary>
    /// 锁定结束时间
    /// </summary>
    [Comment("账号锁定结束时间，null表示未锁定")]
    public DateTimeOffset? LockoutEnd { get; set; }

    /// <summary>
    /// 启用锁定
    /// </summary>
    [Comment("是否启用账号锁定功能")]
    public bool LockoutEnabled { get; set; }

    /// <summary>
    /// 访问失败次数
    /// </summary>
    [Comment("登录失败次数，超过限制将锁定账号")]
    public int AccessFailedCount { get; set; }

    /// <summary>
    /// 是否需要重置密码
    /// </summary>
    [Comment("是否需要重置密码")]
    public bool RequiresPasswordReset { get; set; }

    /// <summary>
    /// 最后密码修改时间
    /// </summary>
    [Comment("最后密码修改时间")]
    public DateTime? LastPasswordChangedTime { get; set; }

    /// <summary>
    /// 真实姓名
    /// </summary>
    [MaxLength(50)]
    [Comment("用户真实姓名")]
    public string RealName { get; set; }

    /// <summary>
    /// 用户昵称
    /// </summary>
    [MaxLength(50)]
    [Comment("用户昵称，显示名称")]
    public string NickName { get; set; }

    /// <summary>
    /// 性别(0-未知，1-男，2-女)
    /// </summary>
    [Comment("用户性别：0-未知，1-男，2-女")]
    public int Gender { get; set; }

    /// <summary>
    /// 出生日期
    /// </summary>
    [Comment("用户出生日期")]
    public DateTime? BirthDate { get; set; }

    /// <summary>
    /// 头像URL
    /// </summary>
    [MaxLength(1000)]
    [Comment("用户头像URL")]
    public string AvatarUrl { get; set; }

    /// <summary>
    /// 个人简介
    /// </summary>
    [MaxLength(500)]
    [Comment("用户个人简介")]
    public string Biography { get; set; }

    /// <summary>
    /// 最后登录时间
    /// </summary>
    [Comment("最后登录时间")]
    public DateTime? LastLoginTime { get; set; }

    /// <summary>
    /// 最后登录IP
    /// </summary>
    [MaxLength(128)]
    [Comment("最后登录IP地址")]
    public string LastLoginIp { get; set; }

    /// <summary>
    /// 用户类型
    /// </summary>
    [Comment("用户类型：0-普通用户，1-管理员，2-系统用户")]
    public UserType UserType { get; set; } = UserType.Normal;

    /// <summary>
    /// 用户状态
    /// </summary>
    [Comment("用户状态：0-正常，1-已禁用，2-已锁定，3-未激活")]
    public UserStatus Status { get; set; } = UserStatus.Normal;

    /// <summary>
    /// 用户角色关系
    /// </summary>
    [InverseProperty("User")]
    public virtual ICollection<UserRole> UserRoles { get; set; }

    /// <summary>
    /// 用户声明
    /// </summary>
    [InverseProperty("User")]
    public virtual ICollection<UserClaim> UserClaims { get; set; }

    /// <summary>
    /// 密码重置令牌
    /// </summary>
    [InverseProperty("User")]
    public virtual ICollection<PasswordResetToken> PasswordResetTokens { get; set; }
}